<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkRoom\Action\Sidebar;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\SidebarAuthMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactRoomContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\App\WorkRoom\Contract\WorkRoomContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\RoomTag\Contract\RoomTagContract;
use MoChat\Plugin\RoomTag\Contract\RoomTagPivotContract;

/**
 * 客户画像-基础信息.
 *
 * Class Show
 * @Controller
 */
class Show extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var WorkRoomContract
     */
    protected $workRoom;

    /**
     * @Inject
     * @var WorkContactRoomContract
     */
    protected $workContactRoom;

    /**
     * @Inject
     * @var RoomTagPivotContract
     */
    protected $workRoomTagPivot;

    /**
     * @Inject
     * @var RoomTagContract
     */
    protected $workRoomTag;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeContract;

    /**
     * @Middlewares({
     *     @Middleware(SidebarAuthMiddleware::class)
     * })
     * @RequestMapping(path="/sidebar/workRoom/show", methods="get")
     */
    public function handle(): array
    {
        $user = user();
        # # 判断用户绑定企业信息
        if (! isset($user['corpId'])) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        // 接收参数
        $params = [
            'wxChatId' => $this->request->input('wxChatId'),
        ];
        // 校验参数
        $this->validated($params);
        $info = $this->workRoom->getWorkRoomByCorpIdWxChatId($user['corpId'], $params['wxChatId'], ['id', 'name', 'description', 'create_time']);
        $contactMan = $this->workContactRoom->countWorkContactRoomsByRoomIdGender($info['id'], 1);
        $employeeRoom = $this->workContactRoom->getWorkContactRoomsByRoomIdEmployee($info['id'], ['employee_id']);
        $employeeMan = empty($employeeRoom) ? 0 : $this->workEmployeeContract->countWorkEmployeesByIdsGender(array_column($employeeRoom, 'employeeId'), 1);
        $info['man'] = $contactMan + $employeeMan;
        $contactFemale = $this->workContactRoom->countWorkContactRoomsByRoomIdGender($info['id'], 2);
        $employeeFemale = empty($employeeRoom) ? 0 : $this->workEmployeeContract->countWorkEmployeesByIdsGender(array_column($employeeRoom, 'employeeId'), 2);
        $info['female'] = $contactFemale + $employeeFemale;
        $contactUnknown = $this->workContactRoom->countWorkContactRoomsByRoomIdGender($info['id'], 0);
        $employeeUnknown = empty($employeeRoom) ? 0 : $this->workEmployeeContract->countWorkEmployeesByIdsGender(array_column($employeeRoom, 'employeeId'), 0);
        $info['unknown'] = $contactUnknown + $employeeUnknown;
        $info['total'] = $this->workContactRoom->countWorkContactRoomByRoomId($info['id'], 0, 1);
        $info['todayInsert'] = $this->workContactRoom->countAddWorkContactRoomsByRoomIdTime([$info['id']], date('Y-m-d'), date('Y-m-d', strtotime('+1 day')));
        $info['todayLoss'] = $this->workContactRoom->countQuitWorkContactRoomsByRoomIdTime([$info['id']], date('Y-m-d'), date('Y-m-d', strtotime('+1 day')));
        $info['todayActive'] = 0; // 今日活跃待完善
        # # 群标签 TODO 改为分开调用，核心尽量不调用插件
        $roomTag = $this->workRoomTagPivot->getRoomTagPivotsByRoomIdTagId($info['id'], 0, ['room_tag_id']);
        $tagName = $this->workRoomTag->getRoomTagsById(array_column($roomTag, 'roomTagId'), ['name']);
        $info['tagName'] = array_column($tagName, 'name');
        return $info;
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'wxChatId' => 'required',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'wxChatId.required' => '客户群wxChatId 必传',
        ];
    }
}
